<?php
/**
 * 用户收货地址服务层
 * @author maniac
 */
namespace app\common\service;

use app\common\model\MemberAddresses AS MemberAddressesModel;

class MemberAddresses extends Base
{
    private $field = 'id,name,mobile,province,city,district,address,is_default,postcode';

    /**
     * 获取用户默认收货地址
     * @param int $userId 用户 id
     */
    public function defAddr($userId)
    {
        $where = [
            'userid'     => $userId,
            'is_default' => 1,
        ];
        $info = MemberAddressesModel::field($this->field)->append(['province_txt','city_txt','district_txt'])->where($where)->find();
        if (empty($info)){
            $info = MemberAddressesModel::field($this->field)->append(['province_txt','city_txt','district_txt'])->where("userid",$userId)->find();
        }
        if ($info) {
            $info['mobile'] = substr($info['mobile'], 0, 3).'****'.substr($info['mobile'], -4);
        }
        return $info;
    }
    
    /**
     * 设置用户默认收货地址
     * @param int $userId 用户 id
     * @param int $id     收货地址 id
     */
    public function setDefAddr($userId, $id)
    {
        $where = [
            'id'     => $id,
            'userid' => $userId,
        ];
        $info = MemberAddressesModel::field($this->field)->append(['province_txt','city_txt','district_txt'])->where($where)->find();
        if (!$info) {
            exception('收货地址不存在');
        }
        $info->is_default = 1;
        $info->save();
        $info['mobile'] = substr($info['mobile'], 0, 3).'****'.substr($info['mobile'], -4);
        $where = [
            ['id', 'neq', $id],
            ['userid', 'eq', $userId],
        ];
        MemberAddressesModel::where($where)->update(['is_default' => 0]);
        return $info;
    }
    
    /**
     * 获取用户收货地址列表信息
     * @param int $userId 用户 id
     */
    public function addresses($userId)
    {
        $where = [
            'userid' => $userId,
        ];
        return MemberAddressesModel::field($this->field)->append(['province_txt','city_txt','district_txt'])->where($where)->order('is_default DESC,id DESC')->select();
    }
    
    /**
     * 获取用户收货地址信息
     * @param int $userId 用户 id
     * @param int $id     收货地址 id
     */
    public function address($userId, $id)
    {
        $where = [
            'id'     => $id,
            'userid' => $userId,
        ];
        return MemberAddressesModel::field($this->field)->append(['province_txt','city_txt','district_txt'])->where($where)->find();
    }
    
    /**
     * 编辑用户收货地址
     * @param  array $data 请求的信息
     * @return int
     */
    public function editAddress($data)
    {
        $id = (int)$data['id'];
        if ($id) {
            $info = MemberAddressesModel::field($this->field)->append(['province_txt','city_txt','district_txt'])->where('id', $id)->find();
        }
        if ($id && $info) {
            $info->name       = $data['name'];
            $info->mobile     = $data['mobile'];
            $info->province   = $data['province'];
            $info->city       = $data['city'];
            $info->district   = $data['district'];
            $info->address    = $data['address'];
            $info->is_default = $data['is_default'];
            $info->postcode = $data['postcode'];
            $info->save();
            if (isset($data['is_default']) && $data['is_default']) {
                $defId = $data['id'];
            }
        } else {
            $info = MemberAddressesModel::create($data);
            if (isset($data['is_default']) && $data['is_default']) {
                $defId = $info->id;
            }
        }
        if (isset($defId) && $defId) {
            $where = [
                ['id', 'neq', $defId],
                ['userid', 'eq', $data['userid']],
            ];
            MemberAddressesModel::where($where)->update(['is_default' => 0]);
        }
        return $this->defAddr($data['userid']);
    }
    
    /**
     * 删除收货地址
     * @param int $id     收货地址 id
     * @param int $userId 用户 id
     */
    public function delAddress($id, $userId)
    {
        $where = [
            'id'     => $id,
            'userid' => $userId,
        ];
        $info = MemberAddressesModel::field($this->field)->append(['province_txt','city_txt','district_txt'])->where($where)->find();
        if (!$info) {
            exception('收货地址不存在');
        }
        $result = [];
        if ($info['is_default']) {
            $addr = MemberAddressesModel::field($this->field)->append(['province_txt','city_txt','district_txt'])->where(['userid' => $userId, 'is_default' => 0])->order('id DESC')->find();
            if ($addr) {
                $addr->is_default = 1;
                $addr->save();
                $addr['mobile'] = substr($addr['mobile'], 0, 3).'****'.substr($addr['mobile'], -4);
                $result = $addr;
            }
        }
        MemberAddressesModel::destroy($id);
        return $result;
    }
}
